package org.wjw;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/status")
public class StatusCodeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset = utf-8");
        //获取参数
        String type = req.getParameter("type");
        //根据不同的值返回不同状态码
        if (type.equals("1")){
            resp.setStatus(200);
        }else if (type.equals("2")){
            resp.setStatus(404);
        }else if (type.equals("3")){
            resp.setStatus(403);
        }else if (type.equals("4")){
            resp.setStatus(500);
        }else {
            resp.setStatus(501);
        }

        resp.getWriter().write("返回的状态码是：" + resp.getStatus());
    }
}
